import axios from "axios";
import NProgress from 'nprogress'
import {
    UserInfo,
    ICategoriesInfo,
    IAddCateParams,
    IUpdateCateInfo,
    IGoodsListInfo,
    IOrderListInfo
} from "@/util/interface";

// 全局配置axios
axios.defaults.baseURL = 'https://lianghj.top:8888/api/private/v1/'
// axios.defaults.baseURL = 'http://82.157.53.213:8887/api/private/v1/'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// 请求拦截器
axios.interceptors.request.use((config:any) => {
    // console.log(config)
    NProgress.start()
    config.headers.Authorization = window.sessionStorage.getItem('token')
    return config
})
axios.interceptors.response.use((config:any)=>{
    NProgress.done()
    return config
})

// 登陆
export function Login(loginForm: object){
    return axios.post('login', loginForm)
}
// 退出
export function LogOut(){
    return window.sessionStorage.clear()
}
// 获取菜单列表
export function GetMenuList(){
    return axios.get('menus')
}
// 获取用户列表
export function GetUserList(queryInfo: object){
    // console.log(queryInfo)
    return axios.get('users', {params:queryInfo})
}
// 保存用户状态
export function SaveUserState(userInfo: UserInfo){
    console.log(`users/${userInfo.id}/state/${userInfo.mg_state}`)
    return axios.put(`users/${userInfo.id}/state/${userInfo.mg_state}`)
}
//添加用户
export function AddUser(addForm: object){
    return axios.post('users', addForm)
}
//根据 ID 查询用户信息
export function GetUserById(id: number) {
    return axios.get(`users/${id}`)
}
//编辑用户提交的信息
export function EditUser(edit:any) {
    return axios.put(`users/${edit.id}`, {
        mobile: edit.mobile,
        email: edit.email
    })
}
//删除单个用户
export function DeleteUser(id: number) {
    return axios.delete(`users/${id}`)
}
//分派用户角色
export function DistributeRole(id: number, rId: number|null) {
    console.log(id, rId)
    return axios.put(`users/${id}/role`, {rid: rId})
}
//所有权限列表
export function AllRights(){
    return axios.get(`rights/list`)
}
//获取所有权限树形图
export function AllRightsByTree() {
    return axios.get(`rights/tree`)
}
// 所有角色列表
export function AllRoles() {
    return axios.get(`roles`)
}
// 添加角色
export function AddRole(addRoleForm: object) {
    return axios.post(`roles`, addRoleForm)
}
//根据 ID 查询角色
export function SelectRoleById(id: number){
    return axios.get(`roles/${id}`)
}
// 编辑角色信息
export function EditRole(id: number, editRoleForm: object) {
    return axios.put(`roles/${id}`, editRoleForm)
}
//指定id 删除角色
export function DeleteRole(id: number) {
    return axios.delete(`roles/${id}`)
}
//删除角色指定权限
export function DeleteRoleRights(roleId: number, rightId: number) {
    return axios.delete(`roles/${roleId}/rights/${rightId}`)
}
//角色授权
export function SetRoleRights(roleId: number, rids: string) {
    return axios.post(`roles/${roleId}/rights`, {rids: rids})
}
//获取商品分类数据列表(一级二级三级)
export function GetCategoriesList(queryParams?: ICategoriesInfo) {
    return axios.get('categories', {params: queryParams})
}
//删除商品分类
export function DeleteCategories(id: number) {
    return axios.delete(`categories/${id}`)
}
//添加商品分类
export function AddCategories(cateParams: IAddCateParams) {
    return axios.post(`categories`, cateParams)
}
//根据 id 查询分类
export function GetCategoriesById(id: number) {
    return axios.get(`categories/${id}`)
}
//修改商品分类信息
export function UpdateCategories(updateInfo: IUpdateCateInfo) {
    return axios.put(`categories/${updateInfo.cat_id}`, {cat_name: updateInfo.cat_name})
}
//获取分类参数列表
export function GetCategoriesParamsList(id: number | null, para: object) {
    return axios.get(`categories/${id}/attributes`, {params: para})
}
//添加动态参数或者静态属性
export function AddDynamicOrStatic(id: number, para: object) {
    return axios.post(`categories/${id}/attributes`, para)
}
//编辑提交参数
export function EditParamsManament(cat_id:number, attrId: number, para: object) {
    return axios.put(`categories/${cat_id}/attributes/${attrId}`, para)
}
//删除参数
export function DeleteParamsManament(cat_id:number, attrId: number) {
    return axios.delete(`categories/${cat_id}/attributes/${attrId}`)
}
// 获取商品列表
export function GetGoodsList(goodsInfo: IGoodsListInfo) {
    return axios.get(`goods`, {params: goodsInfo})
}
// 删除商品
export function DeleteGoodsById(id: number) {
    return axios.delete(`goods/${id}`)
}
// 添加商品
export function AddGoods(goodsInfo: object){
    return axios.post(`goods`, goodsInfo)
}
// 获取所有订单
export function GetOrderList(orderInfo: IOrderListInfo) {
    return axios.get(`orders`, {params: orderInfo})
}
// 获取物流信息
export function GetLogistic(id: number = 1106975712662) {
    return axios.get(`kuaidi/${id}`)
}
// 基于时间统计的折线图
export function GetReports() {
    return axios.get(`reports/type/1`)
}